package com.springBoot.springBootSysCore.modules.entity.system;


import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.springBoot.springBootSysCore.common.comment.annotation.ManyToManyComment;
import com.springBoot.springBootSysCore.common.comment.annotation.MyComment;
import com.springBoot.springBootSysCore.common.persistence.DataEntity;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 * 角色表
 */
@Entity
@Table(name="SYS_ROLE",
uniqueConstraints=@UniqueConstraint(columnNames="CODE"))
@MyComment("角色表")
public class SysRole extends DataEntity<SysRole>{
	private static final long serialVersionUID = 1L;
	private String name;//名称
	private String remark;//描述
	private String code;//code
	private Boolean isSysRole=Boolean.FALSE;//是否是系统预留角色，预留角色不可删除
	private List<SysUser> users;//用户
	private List<SysUser> defalutUsers;//用户
	private List<SysResource> resources;//资源
	
	private String dataScope;// 数据范围
	// 数据范围（1：所有数据；2：所在公司及以下数据；3：所在公司数据；4：所在部门及以下数据；5：所在部门数据；8：仅本人数据；9：按明细设置）
//	public static final String DATA_SCOPE_ALL = "1";
//	public static final String DATA_SCOPE_COMPANY_AND_CHILD = "2";
//	public static final String DATA_SCOPE_COMPANY = "3";
//	public static final String DATA_SCOPE_OFFICE_AND_CHILD = "4";
//	public static final String DATA_SCOPE_OFFICE = "5";
//	public static final String DATA_SCOPE_SELF = "8";
//	public static final String DATA_SCOPE_CUSTOM = "9";

	@Column(name="NAME",length=32)
	@MyComment("角色名称")
	@NotNull(message="角色名称")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(name="REMARK",length=256)
	@MyComment("角色描述")
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	@Column(name="CODE")
	@MyComment("角色CODE")
	@NotNull(message="角色CODE不能为空")
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	@Column(name="IS_SYSROLE")
	@MyComment("是否是系统预留角色，预留角色不可删除 ")
	public Boolean getIsSysRole() {
		return isSysRole;
	}
	public void setIsSysRole(Boolean isSysRole) {
		this.isSysRole = isSysRole;
	}
	
	@ManyToMany(fetch = FetchType.LAZY,mappedBy="roleList")
	@JsonIgnore
	public List<SysUser> getUsers() {
		return users;
	}
	public void setUsers(List<SysUser> users) {
		this.users = users;
	}	
	@OneToMany(fetch = FetchType.LAZY,mappedBy = "defalutRole")
//	@JoinColumn(name="DEFALUT_ROLE")
	@JsonIgnore
	public List<SysUser> getDefalutUsers() {
		return defalutUsers;
	}
	public void setDefalutUsers(List<SysUser> defalutUsers) {
		this.defalutUsers = defalutUsers;
	}

	@ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST},fetch=FetchType.LAZY)
	@JoinTable(
			name="SYS_ROLE_RESOURCE",
			joinColumns={@JoinColumn(name="ROLE_ID")},
			inverseJoinColumns={@JoinColumn(name="RESOURCE_ID")}
	)
	@ManyToManyComment(tableComment="角色与资源关系表",joinColumn="角色ID",inverseJoinColumn="资源ID")
	@JsonIgnore
	public List<SysResource> getResources() {
		return resources;
	}
	public void setResources(List<SysResource> resources) {
		this.resources = resources;
	}
	@Column(name="DATA_SCOPE")
	@MyComment("数据范围")
	public String getDataScope() {
		return dataScope;
	}
	public void setDataScope(String dataScope) {
		this.dataScope = dataScope;
	}

}
